package view
{
	import events.main.EventBrowseForFiles;
	import events.main.EventLoadProject;
	import events.main.EventNewProject;
	import events.main.EventSaveProject;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.IEventDispatcher;
	import flash.events.InvokeEvent;
	
	import flash.events.Event;
	
	import model.ModelBrowseFile;
	import model.ModelProject;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.events.MenuEvent;
	
	import org.robotlegs.base.ContextEvent;
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.WindowedApplication;
	import spark.events.IndexChangeEvent;
	
	import view.base.AbstractTabScreen;
	import mx.managers.PopUpManager;
	
	public class MediatorMainView extends Mediator
	{
		[Inject]
		public var v:MainView;
		
		[Inject]
		public var mp:ModelProject;
		
		[Inject]
		public var mb:ModelBrowseFile;
		
		
		override public function onRegister() : void  {
			
			BindingUtils.bindSetter(setTitle, mp, "appName");
			BindingUtils.bindSetter(setTitle, mp, "version");
			BindingUtils.bindSetter(setTitle, mp, "projectName");
			BindingUtils.bindSetter(setTitle, mp, "projectModified");

			BindingUtils.bindProperty(v.menuBar, "dataProvider", mp, "menuXML");
			
			dispatch(new ContextEvent(ContextEvent.STARTUP_COMPLETE));
			
			eventMap.mapListener(v.menuBar, MenuEvent.ITEM_CLICK, menuSelect);
			
			(FlexGlobals.topLevelApplication as WindowedApplication).addEventListener(Event.CLOSING, mainWindowClosing, false, 0, true);
			eventMap.mapListener(FlexGlobals.topLevelApplication as IEventDispatcher, InvokeEvent.INVOKE, invokeEventHandler);
		}
		
		private function invokeEventHandler(e:InvokeEvent) : void {
			if (e.arguments.length > 0 && e.arguments[0].length > 0) {
				if (mp.projectModified) {
					Alert.show("Project file NOT loaded because current project is not saved!", "Unsaved Project");
				}else {
					dispatch(new EventLoadProject(EventLoadProject.LOAD_PROJECT, e.arguments[0]));
				}
				
			}
			
		}
		
		
		public function mainWindowClosing(e:Event) : void {
			if (mp.projectModified){
				e.preventDefault();
				Alert.show("Unsaved changes will be lost! Are you sure?","Unsaved Project", Alert.YES | Alert.NO, null, killApp);
			}
		}

		private function killApp(e:CloseEvent = null) : void {
			if ((e == null)|| ((e.detail & Alert.YES) > 0)){
				mp.projectModified = false;
				(FlexGlobals.topLevelApplication as WindowedApplication).close();
			}
		}
		
		private function menuSelect(e:MenuEvent) : void {
			if (e.item.@data == "save"){
				dispatch(new EventSaveProject(EventSaveProject.SAVE_PROJECT));
			}else
			if (e.item.@data == "saveas"){
				dispatch(new EventBrowseForFiles(EventBrowseForFiles.BROWSE_FOR_FILES, mb.BROWSE_FOR_SAVE_PROJECT));
			}else
			if (e.item.@data == "load"){
				if (mp.projectModified == true){
					Alert.show("Unsaved changes will be lost! Are you sure?","Unsaved Project", Alert.YES | Alert.NO, null, loadProject);
				}else{
					loadProject();
				}
			}else
			if (e.item.@data == "newproject"){
				if (mp.projectModified == true){
					Alert.show("Unsaved changes will be lost! Are you sure?","Unsaved Project", Alert.YES | Alert.NO, null, newProject);
				}else{
					newProject();
				}
			}else
			if (e.item.@data == "about") {
				var popup:view.PopupAbout = new view.PopupAbout()
				PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
				PopUpManager.centerPopUp(popup);
			}

		}
		
		private function loadProject(e:CloseEvent = null) : void {
			
			if ((e == null)|| ((e.detail & Alert.YES) > 0)){
				dispatch(new EventBrowseForFiles(EventBrowseForFiles.BROWSE_FOR_FILES, mb.BROWSE_FOR_LOAD_PROJECT));
			}
		}
		private function newProject(e:CloseEvent = null) : void {
			if ((e == null) || ((e.detail & Alert.YES) > 0)){
				dispatch(new EventNewProject(EventNewProject.NEW_PROJECT));
			}
		}
		
		private function setTitle(obj:Object) : void {
			FlexGlobals.topLevelApplication.title = mp.appName + " " + mp.version + " - " + mp.projectName + (mp.projectModified?"*" : "");
		}
		
	}
}